//
//  YSGameUserView.m
//  Yaoyle
//
//  Created by ZhaoXudong on 14-9-25.
//  Copyright (c) 2014年 ZhaoXudong. All rights reserved.
//

#import "YSGameUserView.h"
#import "YSUtility.h"
#import "YSType.h"
#import "UIViewExt.h"
#import "YSDefine.h"
#import "YSUi.h"
#import "YSCountDownView.h"

@interface YSGameUserView () {
    UIImageView *headImage;
    UILabel *nameLabel;
    UIImageView *imageView;         // 骰子盒子
    UIImageView *readyStatusView;   // 玩家准备
    NSInteger userId;
    
    YSCountDownView *countDownView;
    UIImageView *diceMsgView;
    
    UILabel *msgLabel;
    UIImageView *msgImage;
}
@end

@implementation YSGameUserView

- (id)initWithUserMOdel:(YSUserModel *)model andPoint:(CGPoint)point {
    self = [super initWithFrame:CGRectMake(point.x, point.y, userViewWidth, userViewHeight)];
    if (self) {
        // Initialization code
        UIImageView *headBg = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"head_kong"]];
        
        headImage = [[UIImageView alloc] init];
        if ([YSUtility isBlankObject:model.photoUrl]) {
            if (model.sex == eSexBoy) {
                headImage.image = [UIImage imageNamed:kDefaultBoyHead];
            } else {
                headImage.image = [UIImage imageNamed:kDefaultGirlHead];
            }
        } else {
            NSURL *url = [NSURL URLWithString:model.photoUrl];
            UIImage *image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
            headImage.image = image;
        }
        
        if (point.x == viewPointX) {
            headImage.frame = CGRectMake(0, 0, 60, 60);
            headBg.frame = CGRectMake(-4, -4, 64, 64);
            countDownView = [[YSCountDownView alloc] initWithPoint:CGPointMake(-5, -5)];
        } else {
            headImage.frame = CGRectMake(userViewWidth - 60, 0, 60, 60);
            headBg.frame = CGRectMake(userViewWidth - 60 -4, -4, 64, 64);
            countDownView = [[YSCountDownView alloc] initWithPoint:CGPointMake(userViewWidth - 65, -5)];
        }

        headImage.layer.masksToBounds = YES;
        headImage.layer.cornerRadius = 30;
        [self addSubview:headBg];
        [self addSubview:countDownView];
        [self addSubview:headImage];
        
        
        nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(headImage.left, headImage.bottom + 10, 60, 14)];
        nameLabel.backgroundColor = [UIColor clearColor];
        nameLabel.textAlignment = NSTextAlignmentCenter;
        nameLabel.font = [UIFont systemFontOfSize:12];
        nameLabel.text = model.name;
        nameLabel.textColor = [UIColor whiteColor];
        [self addSubview:nameLabel];
        
        UIImage *image = [UIImage imageNamed:@"box"];
        imageView = [[UIImageView alloc] initWithImage:image];
        if (point.x == viewPointX) {
            imageView.frame = CGRectMake(headImage.right + 10, headImage.bottom - 35, 35, 35);
        } else {
            imageView.frame = CGRectMake(0, headImage.bottom - 35, 35, 35);
        }
        [self addSubview:imageView];
        
        
        UIImage *image1 = [UIImage imageNamed:@"user_ready"];
        readyStatusView = [[UIImageView alloc] initWithImage:image1];
        if (point.x == viewPointX) {
            readyStatusView.frame = CGRectMake(headImage.right + 10, headImage.top, 45, 20);
        } else {
            readyStatusView.frame = CGRectMake(0, headImage.top, 45, 20);
        }
        [self addSubview:readyStatusView];
        
        if (model.status != 1) {
            [readyStatusView setHidden:YES];
        } else {
            [readyStatusView setHidden:NO];
        }
        
        userId = model.userId;
        
        image = [UIImage imageNamed:@"msg_box_left"];
        diceMsgView = [[UIImageView alloc] init];
        if (point.x == viewPointX) {
            diceMsgView.frame = CGRectMake(imageView.right + 2, headImage.top, 90, 50);
        } else {
            diceMsgView.frame = CGRectMake(imageView.left - 2 - image.size.width, headImage.top, 90, 50);
        }
        [self addSubview:diceMsgView];
        
        msgLabel = [[UILabel alloc] init];
        msgLabel.backgroundColor = [UIColor clearColor];
        msgLabel.textColor = [UIColor whiteColor];
        msgLabel.text = @"2个";
        msgLabel.frame = CGRectMake(8, 10, 40, 20);

        [diceMsgView addSubview:msgLabel];
        
        msgImage = [[UIImageView alloc] init];
        msgImage.image = [UIImage imageNamed:@"bs_1"];
        msgImage.frame = CGRectMake(msgLabel.right + 2, 8, 26, 26);
        
        [diceMsgView addSubview:msgImage];
        [diceMsgView setHidden:YES];
        [readyStatusView setHidden:YES];
        
    }
    return self;
}

- (void)startAnimate {
    CGFloat rotation = 0.3;
    
    CABasicAnimation *shake = [CABasicAnimation animationWithKeyPath:@"transform"];
    shake.duration = 0.13;
    shake.autoreverses = YES;
    shake.repeatCount  = MAXFLOAT;
    shake.removedOnCompletion = NO;
    shake.fromValue = [NSValue valueWithCATransform3D:CATransform3DRotate(imageView.layer.transform,-rotation, 0.0 ,0.0 ,1.0)];
    shake.toValue = [NSValue valueWithCATransform3D:CATransform3DRotate(imageView.layer.transform, rotation, 0.0 ,0.0 ,1.0)];
    
    [imageView.layer addAnimation:shake forKey:@"shakeAnimation"];
}

- (void)stopAnimate {
    [imageView.layer removeAnimationForKey:@"shakeAnimation"];
}

- (NSInteger)getUserId {
    return userId;
}

- (void)showReady {
    [readyStatusView setHidden:NO];
}

- (void)hideReady {
    [readyStatusView setHidden:YES];
}

- (void)showDiceView:(NSInteger)diceValue number:(NSInteger)diceNumber {
    NSLog(@"xxxxxxx  show dice view");
    if (self.frame.origin.x == viewPointX) {
        // 右  msg_box_right
        diceMsgView.image = [UIImage imageNamed:@"msg_box_left"];
    } else {
        // 左  msg_box_left
        diceMsgView.image = [UIImage imageNamed:@"msg_box_right"];
    }
    
    msgLabel.text = [NSString stringWithFormat:@"%d个",diceNumber];
    msgImage.image = [UIImage imageNamed:[NSString stringWithFormat:@"bs_%d",diceValue]];
    [diceMsgView setHidden:NO];
    [readyStatusView setHidden:YES];
    
    [self performSelector:@selector(hidDiceView)
               withObject:nil
               afterDelay:1];
}

- (void)hidDiceView {
    [diceMsgView setHidden:YES];
}

- (void)beginCountDown {
    [countDownView beginCountDown];
}

- (void)stopCountDown {
    [countDownView stopCountDown];
}

@end
